﻿using ProtoBuf.Message;
using System.Collections;
using System.Collections.Generic;
using UI;
using UnityEngine;
using Yunchang;
using Yunchang.NetworkLayer;

public class ChessWeeklyTaskPage : LadderDailyTaskPage
{
    private Dictionary<int, ChessWeeklyTaskItem> cacheTask = new Dictionary<int, ChessWeeklyTaskItem>();
    public static bool HasChessMonthCard = false;
    private bool onceAnimFlag = true;
    public override void NotifyCoverPageRemove()
    {
        RequrestTaskInfo();
    }

    protected override void RequrestTaskInfo()
    {
        NetworkDriver.Instance.SendSocketBlock(OpDefine.CSGetAutoChessWeeklyTaskList, new CSGetAutoChessWeeklyTaskList(), OpDefine.SCGetAutoChessWeeklyTaskList);
    }

    protected override void Register()
    {
        NetworkDriver.Instance.RegistSocketHandler(OpDefine.SCGetAutoChessWeeklyTaskList, OnSCGetAutoChessWeeklyTaskList);
        NetworkDriver.Instance.RegistSocketHandler(OpDefine.SCGetAutoChessTaskReward, OnSCGetAutoChessTaskReward);
    }

    protected override void Unregister()
    {
        NetworkDriver.Instance.UnregisterSocketHandler(OpDefine.SCGetAutoChessWeeklyTaskList, OnSCGetAutoChessWeeklyTaskList);
        NetworkDriver.Instance.UnregisterSocketHandler(OpDefine.SCGetAutoChessTaskReward, OnSCGetAutoChessTaskReward);
    }

    private void OnSCGetAutoChessWeeklyTaskList(object msg)
    {
        var data = msg as SCGetAutoChessWeeklyTaskList;
        float delay = 0f;
        HasChessMonthCard = data.hasMonthCard;
        foreach (var task in data.infos)
        {
            CreateItemExt(delay, task);
            delay += 0.1f;
        }
        CheckRedPoint();
        onceAnimFlag = false;
    }

    private void CheckRedPoint()
    {
        // 检测红点
        bool isAnyRewardCanReceive = false;
        foreach (var taskItem in cacheTask)
        {
            //可领取，并且排除 是月卡任务但是没有月卡不可领取的情况
            if (taskItem.Value.State == (int)AutoChessTaskState.DONE &&
                (!(HasChessMonthCard == false && taskItem.Value.IsMonthCardTask))) 
            {
                isAnyRewardCanReceive = true;
            }
        }
        if (!isAnyRewardCanReceive)
        {
            StaticData.redPointTips.SetStateByType(RedPointType.CHESS_DAILY_TASK_REWARD, false);
        }
    }

    protected void CreateItemExt(float delay, AutoChessTask chessTask)
    {
        GameObject taskItemObj = null;
        ChessWeeklyTaskItem taskItemTemp = null;
        if (cacheTask.ContainsKey(chessTask.taskId))
        {
            taskItemTemp = cacheTask[chessTask.taskId];
        }
        else
        {
            taskItemObj = Instantiate(taskItem, taskItemRoot);
            taskItemTemp = taskItemObj.GetComponent<ChessWeeklyTaskItem>();
        }
        taskItemTemp.gameObject.SetActive(true);
        cacheTask[chessTask.taskId] = taskItemTemp;
        taskItemTemp.IsMonthCardTask = chessTask.taskType == 1;
        taskItemTemp.SetItem(chessTask.state, chessTask.taskId, chessTask.progress, onceAnimFlag, delay);
    }

    private void OnSCGetAutoChessTaskReward(object msg)
    {
        var data = msg as SCGetAutoChessTaskReward;
        if (cacheTask.ContainsKey(data.info.taskId))
        {
            cacheTask[data.info.taskId].IsMonthCardTask = data.info.taskType == 1;
            cacheTask[data.info.taskId].RefreshItem(data.info.state, data.info.taskId, data.info.progress);
        }

        CheckRedPoint();

        PageManager.Instance.OpenPage("GetPage", new GetPageParam()
        {
            rewardList = data.rewards
        });
    }
}
